package com.stedu.mall.goodsservice.controller;

import cn.hutool.core.util.IdUtil;
import com.stedu.mall.common.bean.RespBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@CrossOrigin
@RestController
@RequestMapping({"/category", "/goods"})//上传图片多个地址
public class UploadController {
    @Value("${stedu.picdir}")//注入路径
    private String picdir;

    @PostMapping("/upload")
    public RespBean upload(MultipartFile pic) throws IOException {
        //生成图片的名字---唯一---雪花算法id+后缀
        String filename = pic.getOriginalFilename();//xxx.xxx.xx.xxx
        //获取图片名的后缀 .jpg .png
        String suffix = filename.substring(filename.lastIndexOf("."));
        //生成唯一的文件名
        filename = IdUtil.getSnowflakeNextIdStr() + suffix;
        //将图片保存在服务器
        pic.transferTo(new File(picdir + filename));
        //返回
        return RespBean.ok("上传成功", filename);
    }
}
